package com.smart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smart.entity.Chat;
import com.smart.entity.LoginUser;
import com.smart.mapper.ChatMapper;
import com.smart.service.ChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    ChatMapper chatMapper;
    @Override
    public Integer WorkChatAdd(Chat chat) {
        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (chat.getWorkId() == null && chat.getMessage() == null){
            return 0;
        }
        Long userId = loginUser.getUsers().getUid();
        chat.setUserId(userId);
        chat.setTime(new Date());
        int insert = chatMapper.insert(chat);
        return insert;
    }

    @Override
    public List<Chat> WorkChatAll(Integer id) {
        QueryWrapper<Chat> chatQueryWrapper = new QueryWrapper<>();
        chatQueryWrapper.eq("work_id",id);
        List<Chat> result = chatMapper.selectList(chatQueryWrapper);
        return result;
    }
}
